package network.UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-04-05
 * Time:16:38
 */
public class Receiver {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9999);
        System.out.println("Receiver is blocking at port 9999");
        byte[] buf = new byte[1024 * 54];
        DatagramPacket packet = new DatagramPacket(buf,buf.length);
        socket.receive(packet);
        byte[] data = packet.getData();
        int len = packet.getLength();
        String string = new String(data, 0, len);
        System.out.println(string);
        //====================================================
        byte[] bytes = "Hi Sender!".getBytes();
        packet = new DatagramPacket(bytes, 0, bytes.length, InetAddress.getLocalHost(), 8888);
        socket.send(packet);
        socket.close();
    }
}
